<template>
  <el-dialog :title="title" top="100px" width="500px" append-to-body :visible.sync="dialogedit" @open="openDialog"
    :before-close="closeDialog" :close-on-click-modal="false">
    <div class="form-box">
      <el-form ref="formdata" :model="ruleform" :rules="rules" label-suffix="：" :validate-on-rule-change="false"
        :show-message="false" label-width="140px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="填写日期" prop="rq">
              <el-date-picker v-model="ruleform.rq" type="year" style="width: 100%;" value-format="yyyy"
                placeholder="选择填写日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="护理床位数" prop="hlcw">
              <el-input v-model="ruleform.hlcw" placeholder="请输入护理床位数" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="安宁疗护床位数" prop="ancw">
              <el-input v-model="ruleform.ancw" placeholder="请输入安宁疗护床位数" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="save_validate()">保 存</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import {
    save,
    info
  } from '@/api/jkgl/yyjh';
  export default {
    name: 'Yyjh_edit',

    components: {},
    props: {
      dialogedit: {
        type: Boolean,
        default: false
      },
      id: {
        type: String,
        default: '0'
      }
    },
    data() {
      return {
        title: '新增数据',
        ruleform: {
          id: 0,
          rq: '',
          hlcw: '',
          ancw: ''
        },
        rules: {
          rq: [{
            required: true,
            message: '填报日期不能为空'
          }],
          hlcw: [{
            required: true,
            message: '护理床位数不能为空'
          }],
          ancw: [{
            required: true,
            message: '安宁疗护床位数不能为空'
          }]
        },

      };
    },
    created() {

    },
    methods: {
      closeDialog() {
        this.$nextTick(() => {
          this.$refs.formdata.resetFields();
          this.$emit('update:dialogedit', !this.dialogedit);
        });
      },
      save_validate() {
        this.$refs.formdata.validate((valid, valids) => {
          if (valid) {
            this.saveData();
          } else {
            var msg = '';
            for (var key in valids) {
              var item = valids[key];
              msg = item[0].message;
              break;
            }
            if (msg) this.$message.error(msg);

            return false;
          }
        });
      },

      async saveData() {
        const loading = this.loading('保存中...');
        var params = {}
        params = this.ObjParams(this.ruleform, params);

        save(params).then(response => {
          this.$modal.msgSuccess("保存成功");
          loading.close();
          this.closeDialog();
          this.$emit('reload');
        }).catch(err => {
          loading.close();
        });
      },
      get_info() {
        var Parmas = {
          id: this.id
        }
        info(Parmas).then(res => {
          this.ruleform = {
            id: res.data.id,
            rq: res.data.rq,
            hlcw: res.data.hlcw,
            ancw: res.data.ancw
          }

        });
      },
      async openDialog() {
        if (this.id == '0') {
          this.ruleform = {
            id: 0,
            rq: '',
            hlcw: '',
            ancw: ''
          }
          this.title = "新增数据";
        } else {
          this.get_info();
          this.title = "修改数据";
        }

      }
    }
  };
</script>

<style scoped>
  .form-box {
    min-height: 250px;
    padding-right: 10px;
    overflow: auto;
  }
</style>
